package com.lc.demo;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * @description:java代码连接redis集群
 * @author: ManolinCoder
 * @time: 2021/9/30
 */
public class redisDemo3 {
    public static void main(String[] args) throws IOException {
        // 创建一个集合，保存集群信息
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        nodes.add(new HostAndPort("192.168.133.128", 7001));
        nodes.add(new HostAndPort("192.168.133.128", 7002));
        nodes.add(new HostAndPort("192.168.133.128", 7003));
        nodes.add(new HostAndPort("192.168.133.128", 7004));
        nodes.add(new HostAndPort("192.168.133.128", 7005));
        nodes.add(new HostAndPort("192.168.133.128", 7006));
        //2.创建集群操作对象
        JedisCluster cluster = new JedisCluster(nodes);
        //3.操作数据
        cluster.set("test1", "test111");
        String result = cluster.get("test1");
        System.out.println(result);
        //4.关闭资源
        cluster.close();
    }
}
